package com.tool.btn;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import com.tool.image.ImageMixDeal;
import com.tool.tcp.Sprite;
import org.come.until.CutButtonImage;
import org.come.until.MessagrFlagUntil;
import org.come.until.Util;


public abstract class MoBanBtn extends JLabel implements MouseListener, BtnInterface {
    // 按钮图片
    protected ImageIcon[] icons;
    // 字体颜色
    protected Color[] colors;
    // 按钮是否可用 -1不可用按钮 0不为可用按钮且无图片 1表示非选择按钮 2为绑定选择按钮 3非绑定选择按钮
    protected int btn;
    // 当前图片按钮状态 0移入 1点击 2移出
    protected int zhen;
    protected int imgzhen;
    protected Integer V;// 左上角的小红点
    // 当前按钮状态0移入 1点击 2移出
    // 0移出 1移入 2点击
    protected int type;
    protected int num = -1;
    protected int isup = 0;
    private Sprite sprite;
    private int sx;
    private int sy;
    private boolean ischoose;

    public MoBanBtn(String iconpath, int type) {
        super();
        try {
            btn = type;
            if (iconpath != null) {
                this.icons = CutButtonImage.cuts(iconpath);
                btnchange(0);
            }
            this.addMouseListener(this);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public MoBanBtn(final String iconpath, final int type, final Sprite sprite, int sx, int xy, boolean ischoose) {
        this.num = -1;
        this.isup = 0;
        this.sprite = sprite;
        this.sx = sx;
        this.sy = xy;
        this.ischoose = ischoose;
        try {
            this.btn = type;
            if (iconpath != null) {
                this.icons = CutButtonImage.cuts(iconpath);
                this.btnchange(0);
            }
            this.addMouseListener(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public MoBanBtn(String iconpath, int type, Color[] colors) {
        super();
        try {
            btn = type;
            this.colors = colors;
            if (iconpath != null) {
                this.icons = CutButtonImage.cuts(iconpath);
                btnchange(0);
            }
            this.addMouseListener(this);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public MoBanBtn(String iconpath, int type, Color[] colors, int num, int isup) {
        super();
        try {
            btn = type;
            this.colors = colors;
            this.num = num;
            this.isup = isup;
            if (iconpath != null) {
                this.icons = CutButtonImage.cuts(iconpath);
                btnchange(0);
            }
            this.addMouseListener(this);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void setIschoose(boolean ischoose) {
        this.ischoose = ischoose;
    }
    @Override
    protected void paintComponent(Graphics g) {
        if (type == 2) {
            g.translate(1, 1);
            super.paintComponent(g);
            g.translate(-1, -1);
        } else {
            super.paintComponent(g);
        }
        if (this.sprite != null && ischoose) {
            sprite.updateToTime(ImageMixDeal.userimg.getTime(), 0);
            sprite.draw(g, this.sx, this.sy);
        }
        if (V != null) {
            g.drawImage(CutButtonImage.getJT().getImage(), this.getWidth() - 10, 0, null);
        }
    }

    public MoBanBtn(ImageIcon[] icon, int type) {
        super();
        try {
            btn = type;
            this.icons = icon;
            btnchange(0);
            this.addMouseListener(this);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public MoBanBtn(ImageIcon[] icon, int type, Color[] colors) {
        super();
        try {
            btn = type;
            this.icons = icon;
            this.colors = colors;
            btnchange(0);
            this.addMouseListener(this);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 按钮状态切换
     */
    public void btnchange(int change) {
        zhen = change;
        imgchange(zhen);
    }

    /**
     * 按钮图片切换
     */
    public void imgchange(int change) {
        imgzhen = change;
        if (btn == 0) {
            this.setIcon(null);
        } else {
            if (icons != null) {
                this.setIcon(icons[change]);
            }
            if (colors != null) {
                this.setForeground(colors[change]);
            }
        }
    }

    public void btnchangeWH(int is) {
        if (num != -1) {
            if (is > 1) {
                return;
            }
            if (is < 0) {
                return;
            }
            if (isup == 1) {
                this.setBounds(this.getX(), this.getY() - num, this.getWidth(), this.getHeight() + num);
                isup = 0;
            } else {
                this.setBounds(this.getX(), this.getY() + num, this.getWidth(), this.getHeight() - num);
                isup = 1;
            }
            this.repaint();
        }
    }

    /**
     * 切换按钮类型
     */
    public void btntypechange(int change) {
        btn = change;
        type = 0;
        btnchange(1);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
        dianji();
    }

    /**
     * 模拟点击
     */
    public void dianji() {
        if (btn == -1 || btn == 0) {
            return;
        }
        if (btn == 2 || btn == 3) {
            if (zhen != 2) {
                // 选择类生效
                type = 2;
                btnchange(2);
                chooseyes();
            } else if (btn == 3) {
                // 选择类取消
                type = 1;
                btnchange(1);
                chooseno();
            }
        } else {
            type = 2;
            btnchange(2);
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
        shifang(e);
    }

    /**
     * 模拟释放
     */
    public void shifang(MouseEvent e) {
        if (btn == 1) {
            // 非选择类生效
            if (type != 0) {
                type = 0;
                btnchange(type);
                if (Util.isM() && !(e.getSource() instanceof TitlelBtn)) {
                    return;
                }
                nochoose(e);
            } else {
                btnchange(type);
            }
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        if (btn == -1) {
            return;
        }
        if (btn != 0 && zhen != 2) {
            btnchange(1);
        }
        type = 1;
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
        if (btn == -1) {
            return;
        }
        if ((btn != 0 || btn != -1) && zhen != 2) {
            btnchange(0);
        }
        type = 0;
    }

    @Override
    public abstract void chooseyes();

    @Override
    public abstract void chooseno();

    @Override
    public abstract void nochoose(MouseEvent e);

    public int getZhen() {
        return zhen;
    }

    public void setZhen(int zhen) {
        this.zhen = zhen;
    }

    public int getImgzhen() {
        return imgzhen;
    }

    public void setImgzhen(int imgzhen) {
        this.imgzhen = imgzhen;
    }

    public ImageIcon[] getIcons() {
        return icons;
    }

    public void setIcons(ImageIcon[] icons) {
        this.icons = icons;
        btnchange(0);
    }

    public int getBtn() {
        return btn;
    }

    public void setBtn(int btn) {
        this.btn = btn;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getIsup() {
        return isup;
    }

    public void setIsup(int isup) {
        this.isup = isup;
    }

    /**
     * 开启小红点
     */
    public void onOffRed(int v) {
        if (v == 0) {
            V = null;
        } else {
            V = v;
        }
    }

    public Color[] getColors() {
        return colors;
    }

    public void setColors(Color[] colors) {
        setForeground(colors[0]);
        this.colors = colors;
    }

}
